package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackOperationException {

    public static void run(String basePath, List<String[]> context) throws IOException {
        String outputPath = basePath + XcoderConfig.OutputPath + "/exceptions";
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()) {
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        // File writing ###############
        File file = new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath + "/" + className + "OperationException.java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.newLine();
        bw.write("public class "+className+"OperationException extends RuntimeException {\n");
        bw.newLine();
        bw.write("\tprivate static final long serialVersionUID = "+System.currentTimeMillis()+"L;\n");
        bw.newLine();
        bw.write("\tpublic "+className+"OperationException(String msg) {\n");
        bw.write("\t\tsuper(msg);\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("}\n");
        bw.close();
    }
}
